import { Component, OnInit, ViewChild } from '@angular/core';
import { STChange, STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { EAEnvironmentService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { DataService } from '../../../services/data.service';

@Component({
  selector: 'app-datatable-partnertable',
  templateUrl: './partnertable.component.html'
})
export class DatatablePartnertableComponent implements OnInit {
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  _$expand = false;
  ui!: SFUISchema;
  schema!: SFSchema;
  phone = '';
  columns!: STColumn[];
  isLoading: boolean = false;

  constructor(
    public service: DataService,
    private modalSrv: NzModalService,
    private modal: ModalHelper,
    private envSrv: EAEnvironmentService
  ) {}

  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return { ...params };
  }

  /**
   * 选中行
   */
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
    this.isLoading = true;
  }
  /**
   * 程序初始化入口
   */
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        name: {
          title: '合伙人名称',
          type: 'string',
          ui: { placeholder: '请输入' },
          readOnly: false
        },
        phone: {
          title: '合伙人类型',
          type: 'string',
          ui: {
            widget: 'select',
            placeholder: '请输入'
          },
          readOnly: false
        },
        phone01: {
          title: '合伙人状态',
          type: 'string',
          ui: {
            widget: 'select',
            placeholder: '请输入'
          },
          readOnly: false
        },
        createTime: {
          type: 'string',
          title: '注册时间',
          ui: { widget: 'sl-from-to', type: 'date', format: 'yyyy-MM-dd', visibleIf: {
            _$expand: (value: boolean) => value,
          }, } as SFDateWidgetSchema
        }
      },
      type: 'object'
    };
    this.ui = { '*': { spanLabelFixed: 120, grid: { span: 8, gutter: 4 }, enter: () => this.st?.load(1) } };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      { title: '合伙人名称', index: 'networkTransporterName', className: 'text-center' },
      { title: '注册时间', index: 'telephone', className: 'text-center' },
      { title: '注册时间', index: 'roleName', className: 'text-center' },
      { title: '业务员', index: 'lastLoginDate', className: 'text-center' },
      {
        title: '合伙人状态',
        index: 'stateLocked',
        className: 'text-center',
        type: 'enum',
        enum: { 0: '正常', 1: '冻结' }
      },
      { title: '客户数', index: 'lastLoginDate', className: 'text-center' },
      { title: '收益额', index: 'lastLoginDate', className: 'text-center' },
      { title: '已提现金额', index: 'lastLoginDate', className: 'text-center' },
      { title: '订单数', index: 'lastLoginDate', className: 'text-center' },
      { title: '订单金额', index: 'lastLoginDate', className: 'text-center' },
      { title: '应收订单数', index: 'lastLoginDate', className: 'text-center' }
    ];
  }

  /**
   * 数据列表状态变化事件
   */
  change(change: STChange) {
    // console.log(change);
  }
}
